package com.bening.smsapp.parser.thread.worker;


import com.bening.smsapp.bootstrap.BootConfigParameters;
import com.bening.smsapp.constant.SmsAppConstant;
import com.bening.smsapp.dao.SmsAppDao;
import com.bening.smsapp.factory.SmsAppFactory;

import com.bening.smsapp.util.SmsAppTools;
import com.jp.xml.asn1.*;
import com.ibm.xml.parser.DTD;
import com.ibm.xml.parser.Parser;

import java.io.*;

import java.sql.SQLException;

import org.apache.log4j.Logger;

public class Asn1ParserWorker {
	
	private static BootConfigParameters bootParam = BootConfigParameters.getInstance();
	final static Logger logger = Logger.getLogger( Asn1ParserWorker.class );
	private static SmsAppDao smsDao = SmsAppFactory.getDaoObject();
	
    public void parseFileToDatabase( String folder, String filename , int sourcePathId)
    {
        final String METHOD_SIGNATURE = "[parser]";
        
        // update start processing time
        try  {
        	SmsAppTools.updateFileStatus(filename, SmsAppConstant.FLAG_PARSE_RUNNING);
        }catch(SQLException s) {
        	logger.error(s);
        	SmsAppTools.InsertEventLog(SmsAppConstant.SQL_ERROR, s.getMessage(), sourcePathId);
        }

        try {
        	runParser(folder, filename);
        	//smsDao.insertParsingResultAsn1(decodeValue(b), filename);
        	
        }catch(SQLException s) {
        	logger.error(s);
        	SmsAppTools.InsertEventLog(SmsAppConstant.SQL_ERROR, s.getMessage(), sourcePathId);
    	}catch(Exception i) {
    		logger.error(i);
    		SmsAppTools.InsertEventLog(SmsAppConstant.PARSE_ERROR, i.getMessage(), sourcePathId);
    	}

        // update end processing time
        try  {
        	SmsAppTools.updateFileStatus(filename, SmsAppConstant.FLAG_PARSE_DONE);
        }catch(SQLException s) {
        	logger.error(s);
        	SmsAppTools.InsertEventLog(SmsAppConstant.SQL_ERROR, s.getMessage(), sourcePathId);
        }
        
    }

    private synchronized void runParser(String folder, String filename) throws Exception {
       
        //SAXEventDumper dump = new SAXEventDumper();
    	CSVEventDumper dump = new CSVEventDumper();
    	String tempdir = bootParam.getTempDir()+"/"+ filename +".TMP";
    	Writer out = new StringWriter();
/*        if (tempdir != null) {
            out = new FileWriter(tempdir);
          }
*/          
        dump.setWriter(out);
        try {
	    
        	com.jp.xml.asn1.Asn12Csv a2s = new com.jp.xml.asn1.Asn12Csv(dump);
        	//com.jp.xml.asn1.ASN12SAX a2s = new com.jp.xml.asn1.ASN12SAX(dump);
	        a2s.setPartialDecoding(false);
	        InputStream in = new FileInputStream(folder + File.separator + filename);
	        a2s.setInputStream(in);
	        a2s.setEncoding("base64");
	        a2s.start();
	        in.close();
	        //out.write("</ROOT>");
	        //out.flush();
	        //out.close();
	        logger.info("Writing csv to "+tempdir);
	        a2s.writeToCsv(tempdir);
	        
        }catch(Exception f) {
	    	  logger.error(f);
	    	  //throw f;
	    }	    
    }
    
    private static DTD readDTD(String name) throws IOException {
        Parser psr = new Parser(name);
        Reader in = new FileReader(name);
        DTD dtd = psr.readDTDStream(in);
        return dtd;
      }

}
